package com.guochuang.yunwang.promanager.serviceImpl;

import com.guochuang.yunwang.promanager.dao.UserGroupMapper;
import com.guochuang.yunwang.promanager.domain.UserGroup;
import com.guochuang.yunwang.promanager.domain.dto.group.GroupManageDTO;
import com.guochuang.yunwang.promanager.domain.vo.group.GroupUserVO;
import com.guochuang.yunwang.promanager.exception.ParamException;
import com.guochuang.yunwang.promanager.service.UserGroupService;
import com.guochuang.yunwang.promanager.utils.ProjectConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserGroupServiceImpl implements UserGroupService {
    @Autowired
    private UserGroupMapper userGroupMapper;

    @Override
    public Integer addUserGroup(UserGroup userGroup){
        return userGroupMapper.addUserGroup(userGroup);
    }

    @Override
    public Integer updateUserGroup(UserGroup userGroup){
        return userGroupMapper.updateUserGroup(userGroup);
    }

    @Override
    public UserGroup getUserGroupByGId(Integer gid){
        return userGroupMapper.getUserGroupByGId(gid);
    }

    @Override
    public List<Integer> selectAllRidsByGid(Integer gid){
        return userGroupMapper.selectAllRidsByGid(gid);
    }

    @Override
    public List<GroupManageDTO> selectGroupManageDTOsInGroupByGid(Integer gid){
        return userGroupMapper.selectGroupManageDTOsInGroupByGid(gid);
    }

    @Override
    public List<GroupManageDTO> selectAllGroupManageDTOs(){
        return userGroupMapper.selectAllGroupManageDTOs();
    }

    @Override
    public List<Integer> selectAllUidsInGroupByGroupId(Integer id){
        return userGroupMapper.selectAllUidsInGroupByGroupId(id);
    }

    @Override
    @Transactional
    public Integer addUserToGroup(Integer gid, List<Integer> uids) {
        if(uids == null || uids.size()==0 || gid==null){
            throw new ParamException(ProjectConstant.PARAM_ERROR_MESSAGE);
        }

        // 首先获取团队中已有的人
        List<Integer> inuids = userGroupMapper.getGroupUserIdList(gid);

        List<Integer> adduids = new ArrayList<>();

        List<Integer> deluids = new ArrayList<>();

        // 组装成map
        Map<Integer,Object> oldinuidsmap = new HashMap<>();

        for (Integer id : inuids) {
            oldinuidsmap.put(id,"");
        }
        // 目前团队的人数
        Map<Integer,Object> newinuidsmap = new HashMap<>();

        for (Integer uid : uids) {
            newinuidsmap.put(uid,"");
        }
        // 筛选出需要删除的人
        for (Integer inuid : inuids) {
            if(newinuidsmap.get(inuid)==null){
                deluids.add(inuid);  // 若为空则表示需要删除
            }
        }
        // 筛选出需要添加的人
        for (Integer uid : uids) {
            if(oldinuidsmap.get(uid)==null){
                adduids.add(uid);
            }
        }
        if(adduids.size()!=0){
            // 添加用户
            userGroupMapper.addUsersToGroup(gid, adduids);
        }
        if(deluids.size()!=0){
            // 清除用户
            userGroupMapper.deleteGroupUserList(gid,deluids);
        }
        // 修改团队的人数
        Integer persons = inuids.size() + adduids.size() -deluids.size();

        return userGroupMapper.updateGroupNums(gid,persons);
    }

    @Override
    public List<GroupUserVO> getGroupUsersList(Integer gid) {
        return userGroupMapper.getGroupUserLists(gid);
    }

    @Override
    public Integer deleteGroupUser(Integer gid, Integer uid) {
        return userGroupMapper.deleteGroupUser(gid,uid);
    }

    @Override
    public Integer updateGroupUserRole(Integer uid, Integer gid, Integer rid) {
        return userGroupMapper.updateGroupUserRole(uid,gid,rid);
    }
}
